<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
function getTableHeader($key)
{
    $userInfo = getLoginInfo();

    $user_header = ['id' => 'id', 'company_id' => '归属公司', 'mobile' => '手机号', 'create_time' => '创建时间', 'last_login_time' => '最后登录时间', 'status' => '状态'];
    $user_fields = ['id', 'company_id', 'office_id', 'login_name', 'no', 'name', 'email', 'phone', 'mobile', 'user_type', 'photo', 'create_date', 'login_flag'];
    $where_must = ['del_flag' => 0];

    $menu_header = ['id' => 'id', 'company_id' => '归属公司', 'mobile' => '手机号', 'create_time' => '创建时间', 'last_login_time' => '最后登录时间', 'status' => '状态'];
    $menu_fields = ['id', 'parent_id', 'parent_ids', 'name', 'type', 'sort', 'href', 'target', 'icon', 'href1', 'target1', 'icon1', 'href2', 'target2', 'icon2', 'is_show', 'menu_permission', 'btn_permission', 'interface_permission', 'remarks'];
    $menu_where_must = ['del_flag' => 0];


    $dict_header = ['id' => 'id', 'company_id' => '归属公司', 'mobile' => '手机号', 'create_time' => '创建时间', 'last_login_time' => '最后登录时间', 'status' => '状态'];
    $dict_fields = ['id', 'office_id', 'name', 'enname', 'role_type', 'data_scope', 'is_sys', 'useable', 'create_by', 'create_date', 'update_by', 'update_date', 'remarks', 'auth_start', 'auth_end'];
    $dict_where_must = ['del_flag' => 0];

    $role_header = ['id' => 'id', 'company_id' => '归属公司', 'mobile' => '手机号', 'create_time' => '创建时间', 'last_login_time' => '最后登录时间', 'status' => '状态'];
    $role_fields = ['id', 'value', 'label', 'type', 'type_category', 'description', 'sort', 'parent_id', 'remarks', 'dict_type'];
    $role_where_must = ['del_flag' => 0];

    $office_header = ['id' => 'id', 'company_id' => '归属公司', 'mobile' => '手机号', 'create_time' => '创建时间', 'last_login_time' => '最后登录时间', 'status' => '状态'];
    $office_fields = ['id', 'parent_id', 'parent_ids', 'name', 'sort', 'area_id', 'code', 'type', 'grade', 'address', 'zip_code', 'master', 'phone', 'fax', 'email', 'useable', 'primary_person', 'deputy_person', 'create_date', 'remarks'];


    $template_header = ['id' => 'id', 'company_id' => '归属公司', 'mobile' => '手机号', 'create_time' => '创建时间', 'last_login_time' => '最后登录时间', 'status' => '状态'];
    $template_fields = ['id', 'content', 'title', 'is_site', 'create_time'];

    $user_header = [
        ['title' => '编号', 'type' => 'selection', 'align' => 'center', 'minWidth' => 60],
        ['title' => '登录名称', 'key' => 'login_name', 'align' => 'center','minWidth' => 150, 'slot'=>'showChild'],
        ['title' => '公司名称', 'key' => 'company_name', 'align' => 'center','minWidth' => 100],
        ['title' => '行业', 'key' => 'industry', 'align' => 'center','minWidth' => 100],
        ['title' => '所在地', 'key' => 'address', 'align' => 'center','minWidth' => 100],
        ['title' => '主营产品', 'key' => 'main_product', 'align' => 'center','minWidth' => 100],
        ['title' => '创建时间', 'key' => 'create_time', 'align' => 'center','minWidth' => 100],
        ['title' => '使用状态', 'key' => 'create_time', 'slot' => 'howLong','minWidth' => 100],
        ['title' => '用户状态', 'key' => 'create_time', 'slot' => 'state','minWidth' => 100],
        ['title' => '使用人', 'key' => 'people', 'minWidth' => 100],
        ['title' => '有效期至', 'align' => 'center','slot'=>'showLongTime','minWidth' => 100],
        ['title' => '操作', 'slot' => 'action', 'align' => 'center','minWidth' => 250]
    ];
    $user_fields = ['id', 'pid' ,'login_name', 'company_name', 'industry', 'address', 'main_product','people', 'created_by', 'create_time','how_long_time','pay_time','max_num','del_flag'];
    $user_search = ['login_name' => ['=', 'login_name'],'pid' => ['=', 'pid']];
    $user_must = [];
    if ($userInfo['id'] > 1) {
        $user_must = ['pid' => $userInfo['id']];
        $user_header = [$user_header[0],$user_header[1],$user_header[5],$user_header[6],$user_header[9],$user_header[10],$user_header[11]];
    }


    $order_header = [
        ['title' => '编号', 'type' => 'selection', 'align' => 'center', 'width' => 60],
        ['title' => '订单编号', 'key' => 'order_no', 'align' => 'center'],
        ['title' => '公司', 'key' => 'company_name', 'align' => 'center'],
        ['title' => '行业', 'key' => 'industry', 'align' => 'center'],
        ['title' => '电话', 'key' => 'mobile', 'align' => 'center'],
        ['title' => '地址', 'key' => 'address', 'align' => 'center'],
        ['title' => '订单金额', 'key' => 'money', 'align' => 'center', 'render' => '(h, params) => {
                                                                        let money = params.row.money * 0.01
                                                                        return h(\'div\',money + \'元\')
                                                                    }'
        ],
        ['title' => '订单状态', 'key' => 'type', 'align' => 'center', 'render' => '(h, params) => {
    let text = params.row.type == 2 ? \'已支付\' : \'未支付\'
    let color = params.row.type == 2 ? \'success\' : \'warning\'
    if(params.row.type == 3){
        text = \'失效订单\'
        color = \'error\'
    }
    return h(\'Tag\', {
        props: {
            color: color
        }
    }, text)
}'],
        ['title' => '套餐名称', 'key' => 'title', 'align' => 'center'],
        ['title' => '创建时间', 'key' => 'create_time', 'align' => 'center'],
        ['title' => '支付时间', 'key' => 'pay_time', 'align' => 'center'],
        ['title' => '操作', 'align' => 'center', 'slot' => 'payAction'],
    ];
    if ($userInfo['id'] > 1) {
        $order_header = [$order_header[0],$order_header[1],$order_header[6],$order_header[7],$order_header[8],$order_header[9],$order_header[10],$order_header[11]];
    }
    $order_fields = ['o.id','o.pay_type','o.pay_url', 'o.order_no', 'o.user_id', 'o.money', 'o.type', 'o.create_time', 'o.pay_time', 'o.combo_id', 'o.id', 'c.title', 'c.price', 'c.type as c_type','u.company_name','u.mobile','u.industry','u.address'];
    $order_search = ['o.order_no' => ['=', 'order_no'],'o.type' => ['=', 'type']];
    $order_where_must = [];
    if ($userInfo['login_name'] != 'admin') {
        $order_where_must['o.user_id'] = $userInfo['id'];
    }


    $msg_fields = ['id', 'mobile', 'content', 'create_time'];
    $msg_search = ['mobile' => ['=', 'mobile']];
    $msg_header = [
        ['title' => '编号', 'type' => 'selection', 'align' => 'center', 'width' => 60],
        ['title' => '手机号', 'key' => 'mobile', 'align' => 'center'],
        ['title' => '短信内容', 'key' => 'content', 'align' => 'center'],
        ['title' => '发送时间', 'key' => 'create_time', 'align' => 'center'],
    ];


    $search_log_header = [
        ['title' => '编号', 'type' => 'selection', 'align' => 'center', 'width' => 60],
        ['title' => '名称', 'key' => 'title', 'align' => 'center'],
        ['title' => '序列', 'key' => 'description', 'align' => 'center'],
        ['title' => '跳转地址', 'key' => 'url', 'align' => 'center', 'render' => '(h, params) => {
    let url = params.row.url
    return h(\'a\', {
        domProps: {
            href: url,
            target:\'_blank\'
        }
    },\'点击查看\')
}'
        ],
        ['title' => '去查询', 'key' => 'company', 'align' => 'center','slot'=>'goToSearch'],
        ['title' => '公司', 'key' => 'company', 'align' => 'center'],
        ['title' => '邮箱', 'key' => 'email', 'align' => 'center'],
        ['title' => '网址', 'key' => 'site', 'align' => 'center'],
        ['title' => '查询时间', 'key' => 'create_time', 'align' => 'center'],
    ];
    $search_log_fields = ['id', 'title', 'url', 'company', 'site', 'email', 'other', 'create_time', 'description'];
    $search_log_must = [];
    if ($userInfo['id'] > 1) {
        $search_log_must = ['user_id' => $userInfo['id']];
    }


    //----文章分类的
    $category_header = [
        ['title' => '编号', 'type' => 'selection', 'align' => 'center', 'minWidth' => 60],
        ['title' => '分类名称', 'key' => 'title', 'align' => 'center','minWidth' => 150],
        ['title' => '创建时间', 'key' => 'create_time','minWidth' => 100],
        ['title' => '操作', 'slot' => 'action', 'align' => 'center','minWidth' => 250]
    ];
    $category_fields = ['id','title','create_time','sort','update_time','del_flag'];
    $category_search = ['title' => ['=', 'title']];
    $category_must = ['del_flag'=>0];
//----文章管理的
    $management_header = [
        ['title' => '编号', 'type' => 'selection', 'align' => 'center', 'minWidth' => 60],
        ['title' => '文章名称', 'key' => 'article_name', 'align' => 'center','minWidth' => 150],
        ['title' => '文章作者', 'key' => 'article_author','minWidth' => 100],
        ['title' => '分类名称', 'key' => 'category_name','minWidth' => 100],
        ['title' => '发布时间', 'key' => 'release_time','minWidth' => 100],
        ['title' => '操作', 'slot' => 'action', 'align' => 'center','minWidth' => 250]
    ];
    $management_fields = ['id','article_name','article_author','category_name','release_time','del_flag'];
    $management_search = ['article_name' => ['=', 'article_name']];
    $management_must = ['del_flag'=>0];

    $data['sys_user'] = [$user_header, $user_fields, $where_must];
    $data['sys_menu'] = [$menu_header, $menu_fields, $menu_where_must];
    $data['office'] = [$office_header, $office_fields];
    $data['sys_dict'] = [$dict_header, $dict_fields, $dict_where_must];
    $data['sys_role'] = [$dict_header, $dict_fields, $dict_where_must];
    $data['lyx_template'] = [$template_header, $template_fields, $menu_where_must,[],['sort'=>'desc']];
    $data['lyx_user'] = [$user_header, $user_fields, $user_must, $user_search];
    $data['lyx_order'] = [$order_header, $order_fields, $order_where_must, $order_search];
    $data['lyx_msg'] = [$msg_header, $msg_fields, [], $msg_search];
    $data['lyx_search_log'] = [$search_log_header, $search_log_fields, $search_log_must, [], ['create_time' => 'desc']];
    $data['article_category'] = [$category_header, $category_fields, $category_must,$category_search];
    $data['shenbei_site_management_articles'] = [$management_header, $management_fields, $management_must,$management_search];
    return $data[$key];
}
/**
 * @param $time
 * @param string $format
 *
 * @return false|string
 */
function toDate($time, $format = 'Y-m-d H:i:s')
{
    if (empty($time)) {
        return '';
    }
    $format = str_replace('#', ':', $format);
    return date($format, $time);
}

//树形结构页面函数
function build_tree($rows, $root_id)
{
    $childs = findChild($rows, $root_id);
    if (empty($childs)) {
        return null;
    }
    foreach ($childs as $k => $v) {
        $rescurTree = build_tree($rows, $v['id']);
        if (null != $rescurTree) {
            $childs[$k]['childs'] = $rescurTree;
        }
    }
    return $childs;
}

/*
 * 递归无限菜单
 */
function findChild(&$arr, $id)
{
    $childs = array();
    foreach ($arr as $k => $v) {
        if ($v['pid'] == $id) {
            $childs[] = $v;
        }
    }
    return $childs;
}

/**
 * 把所有数据都过滤成boolean
 *
 * @param $data
 *
 * @return array
 */
function dataBooleanFiltration($data)
{
    $arr = [];
    foreach ($data as $key => $value) {
        if (is_array($value)) {
            $arr[$key] = dataBooleanFiltration($value);
        } else {
            if ($value == 'true') {
                $arr[$key] = true;
            } else if ($value == 'false') {
                $arr[$key] = false;
            } else if (is_numeric($value)) {
                $arr[$key] = (integer)$value;
            } else {
                $arr[$key] = $value;
            }
        }
    }
    return $arr;
}

/**
 * 获取用户的登录信息
 * @return bool|mixed
 */
function getLoginInfo()
{
    $token = \think\facade\Request::cookie('token');
    if (empty($token)) {
        return false;
    } else {
        $info = \think\facade\Cache::get('userLoginInfo_' . $token);
        return empty($info) ? false : $info;
    }
}

function updateLoginInfo()
{
    $user = getLoginInfo();
    $info = \think\facade\Db::name('user')->where('id', $user['id'])->find();
    if ($info != false) {
        $token = \think\facade\Request::cookie('token');
        \think\facade\Cache::set('userLoginInfo_' . $token, $info);
        return true;
    }
    return false;
}

/**
 * 判定是否过期
 * @return bool
 */
function isExpiration()
{
    $userInfo = getLoginInfo();
    if ($userInfo != false) {
        $last = time() - $userInfo['pay_time'];
        if ($last >= $userInfo['how_long_time']) {
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
}